//
//  YZDailyWorkCell.m
//  VET
//
//  Created by Lam BG on 2018/7/11.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZDailyWorkCell.h"

// model
#import "YZAutoItemModel.h"
#import "YZDailyWorkModel.h"

// view
#import "YZStateBtn.h"
#import "YZActionSheet.h"

// api
#import "YZWorkbenchAPI.h"

// 第三方
#import "TBCityIconFont.h"
#import "MJExtension.h"

#import "YZRouter+AutoVC.h"
#import "NSString+DisplayTime.h"
#import "NSDate+YZDatePickView.h"

@interface YZDailyWorkCell ()

@property (weak, nonatomic) IBOutlet YZStateBtn *dwCheckBtn;

@property (weak, nonatomic) IBOutlet UILabel *dwTitleLabel;

@property (weak, nonatomic) IBOutlet UILabel *dwDateLabel;

@property (weak, nonatomic) IBOutlet UILabel *tipLabel;

@property (weak, nonatomic) IBOutlet UILabel *taskTypeLabel;

@property (weak, nonatomic) IBOutlet UILabel *taskObjLabel;

@property (weak, nonatomic) IBOutlet UILabel *completeTimeLabel;

@end

@implementation YZDailyWorkCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    // 取消选中状态
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // 状态按钮
    [self.dwCheckBtn setStateImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\ue641", 30, kColorWithHex(0xD9D9D9))] withSwitchImage:[UIImage iconWithInfo:TBCityIconInfoMake(@"\ue642", 30, kColorWithHex(0x32BE60))]];
}

#pragma mark - setter

- (void)setModel:(YZDailyWorkModel *)model {
    
    _model = model;
    
    self.dwCheckBtn.isSwitch = (self.model.itemType != DWItemTypeUndo);
    self.dwDateLabel.text   = model.showDeadTime;
    self.taskTypeLabel.text = [NSString stringWithFormat:@"类别:%@", model.taskType.itemValue == nil ? @"未确定" : model.taskType.itemValue];
    self.taskObjLabel.text  = [NSString stringWithFormat:@"对象:%@", model.taskObject.itemValue == nil ? @"未确定" : model.taskObject.itemValue];
    
    // 逾期未完成 显示规则 超过 taskDeadline 就显示，因为服务器在凌晨刷新列表
    if (model.itemType == DWItemTypeUndo) {
        
        self.tipLabel.hidden = (model.taskDeadline >= [[NSString currentTimeInterval] doubleValue]);
    } else {
        
        self.tipLabel.hidden = YES;
    }
    
    // 完成的显隐
    self.completeTimeLabel.hidden = !(model.itemType == DWItemTypeDone);
    if (model.itemType == DWItemTypeDone) {
        
        self.completeTimeLabel.text = [NSString stringWithFormat:@"完成时间: %@", [NSDate yz_getDateStringFromTimeInterval:model.gmtModified format:@"yyyy-MM-dd HH:mm"]];
    }
    
    // title 中划线
    if (model.itemType != DWItemTypeUndo) {
        
        //中划线
        NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName : [NSNumber numberWithInteger : NSUnderlineStyleSingle], NSForegroundColorAttributeName : kColorWithHex(0x9B9B9B)};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:kText(model.taskDetail) attributes:attribtDic];
        // 赋值
        self.dwTitleLabel.attributedText = attribtStr;
    } else {
        
        self.dwTitleLabel.text = model.taskDetail;
    }
    
}

#pragma mark - event action

- (IBAction)checkClicked:(UIButton*)sender {
    
//    sender.isSwitch = (self.model.itemType != DWItemTypeDone);
    [self generateActionSheet:self.model.itemType];
}

#pragma private function

- (void)generateActionSheet:(DWItemType)itemType {
    
    YZActionSheet *sheet = nil;
    switch (itemType) {

        case DWItemTypeUndo:
        {
            sheet = [[YZActionSheet alloc] initWithTitle:nil sheetTitles:@[@"编辑修改", @"标记为已完成", @"删除"] cancleBtnTitle:@"取消" sheetStyle:YZActionSheetDefault delegate:nil];
            
            // 编辑修改
            [sheet addAction:^{
                
                // 跳转至编辑页
                NSDictionary *params = [self.model mj_keyValues];
                [[YZRouter sharedInstance] yz_atuoPush:@"dwNewDailyWork" params:@{@"params" : params}];
                
            }];
            
            // 标记为已完成
            [sheet addAction:^{
                
                [self executeMarkDone];
                
            }];

        }
            break;

        case DWItemTypeDone:
        {
            sheet = [[YZActionSheet alloc] initWithTitle:nil sheetTitles:@[@"标记为未完成", @"删除"] cancleBtnTitle:@"取消" sheetStyle:YZActionSheetDefault delegate:nil];

            // 未完成
            [sheet addAction:^{
                
                [self executeMarkUndo];
                
            }];
        }
            break;

        case DWItemTypeIgnored:
        {
            sheet = [[YZActionSheet alloc] initWithTitle:nil sheetTitles:@[@"标记为已完成", @"标记为未完成", @"删除"] cancleBtnTitle:@"取消" sheetStyle:YZActionSheetDefault delegate:nil];
            
            // 标记为已完成
            [sheet addAction:^{
                
                [self executeMarkDone];
                
            }];
            
            // 未完成
            [sheet addAction:^{
                
                [self executeMarkUndo];
                
            }];
            
        }
            break;
    }
    
    // 统一添加删除
    [sheet addAction:^{
        
        NSLog(@"params");
        [self executeDelete];
        
    }];
    
    sheet.sheetHeight = 54;
    [sheet show];
}

- (void)executeMarkDone {
    
    // 执行标记完成
    [[YZWorkbenchAPI markDoneDailyWorkAPI:self.model.dwID] subscribeNext:^(id  _Nullable x) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kUndoRefreshNotification object:@(DWItemTypeDone)];
        if (self.delegate) {
            
            [self.delegate handleDailyWorkChecked:self.indexPath withError:nil];
        }
        
    } error:^(NSError * _Nullable error) {
        
        if (self.delegate) {
            
            [self.delegate handleDailyWorkChecked:self.indexPath withError:error];
        }
    }];
}

- (void)executeMarkUndo {
    
    // 跳转至编辑页
    NSMutableDictionary *params = [self.model mj_keyValues];
    NSDate *date = [NSDate date];
    NSDate *nextDay = [NSDate dateWithTimeInterval:24 * 60 * 60 sinceDate:date];
    NSUInteger timeInterval = [nextDay timeIntervalSince1970];
    [params setObject:@(timeInterval) forKey:@"taskDeadline"];
    [[YZRouter sharedInstance] yz_atuoPush:@"dwNewDailyWork" params:@{@"params" : params}];
//    // 执行标记未完成
//    [[YZWorkbenchAPI markUndoDailyWorkAPI:self.model.dwID] subscribeNext:^(id  _Nullable x) {
//        
//        if (self.delegate) {
//            
//            [self.delegate handleDailyWorkChecked:self.indexPath withError:nil];
//        }
//        
//    } error:^(NSError * _Nullable error) {
//        
//        if (self.delegate) {
//            
//            [self.delegate handleDailyWorkChecked:self.indexPath withError:error];
//        }
//    }];
}

- (void)executeDelete {
    
    // 执行删除
    [[YZWorkbenchAPI deleteDailyWorkAPI:self.model.dwID] subscribeNext:^(id  _Nullable x) {
        
        if (self.delegate) {
            
            [self.delegate handleDailyWorkChecked:self.indexPath withError:nil];
        }
        
    } error:^(NSError * _Nullable error) {
        
        if (self.delegate) {
            
            [self.delegate handleDailyWorkChecked:self.indexPath withError:error];
        }
    }];
}

@end
















